package com.yiren.backstage.judge.services.jjd.impl;

import android.content.Context;

import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.config.enums.UnitNameEnum;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.bean.VoiceBean;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.enums.CarStateValueEnum;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.judge.handler.AutoJudgeHandler;
import com.yiren.backstage.judge.services.jjd.IJjdJudgeThreadService;
import com.yiren.backstage.player.api.IPlayerCallBack;
import com.yiren.backstage.player.api.PlayerApi;

/**
 * Time:2022/4/24 16:02
 * Author:xiaozan
 * Description:加减挡评判线程 服务接口实现
 */
public class JjdJudgeThreadServiceImpl implements IJjdJudgeThreadService {

    private Context context;
    private CarStateBean carStateBean;

    /*** 挂错挡开始时间 */
    private long wrongGearStartTime;
    private boolean isPlay;
    private AutoJudgeHandler autoJudgeHandler;

    public JjdJudgeThreadServiceImpl(Context context, AutoJudgeHandler autoJudgeHandler) {
        this.context = context;
        this.autoJudgeHandler = autoJudgeHandler;
        carStateBean = CarStateBean.getInstance();
    }

    @Override
    public int loadGearOperOrder() {
        int gearOrder = 0;
        switch (carStateBean.getGear()) {
            case ONE_GEAR:
                gearOrder = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J1.toString());
                break;
            case TWO_GEAR:
                gearOrder = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J2.toString());
                break;
            case THREE_GEAR:
                gearOrder = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J3.toString());
                break;
            case FOUR_GEAR:
                gearOrder = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J4.toString());
                break;
            case FIVE_GEAR:
                gearOrder = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J5.toString());
                break;
        }
        return gearOrder;
    }

    @Override
    public boolean playOperVoice(int[] gearOperOrderArray, int index) {
        try {
            String playContent;
            //判断是加挡还是减挡  当前索引值是否大于上次值
            if (gearOperOrderArray[index] > gearOperOrderArray[index - 1]) {
                //加挡
                playContent = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J6.toString());
            } else {
                //降档
                playContent = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J7.toString());
            }
            if (!playContent.equals("")) {
                //将#号替换成实际操作的挡位
                playContent = playContent.replace("#", gearOperOrderArray[index] + "");
                //if (isPlay) return true;
                isPlay = true;
                PlayerApi.playProjectAndJudgeVoice(context, new VoiceBean(playContent, ""), new IPlayerCallBack() {
                    @Override
                    public void playFinish() {
                        isPlay = false;
                    }
                });
                //等待播报完成
                while (isPlay && autoJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
                    Thread.sleep(100);
                }
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean accordGearOper(int operGear) {
        if (carStateBean.getGear().getValue() == operGear) {
            //判断每次挡位操作离合松开才算完成
            if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J10.toString())) {
                if (carStateBean.getSeparationReunion() == CarStateValueEnum.LOOSEN) return true;
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean wrongGear(int[] gearOperOrderArray, int index) {
        //否允许挂错挡位容错
        if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J11.toString())) {
            float faultTolerantTime = (float) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J12.toString());
            //挡位不等于空挡、未知挡、当前需要操作的挡位和上次挡位
            if (carStateBean.getGear().getValue() != gearOperOrderArray[index - 1] && carStateBean.getGear().getValue() != gearOperOrderArray[index] && carStateBean.getGear() != CarStateValueEnum.NEUTRAL_GEAR && carStateBean.getGear() != CarStateValueEnum.UNKNOWN_GEAR) {
                if (wrongGearStartTime == 0) wrongGearStartTime = System.currentTimeMillis();
                if ((System.currentTimeMillis() - wrongGearStartTime) / 1000 > faultTolerantTime)
                    return true;
            } else {
                wrongGearStartTime = 0;
            }
        }
        return false;
    }

    @Override
    public boolean singleGearOperIsOvertime(float startMileage, long startTime) {
        float value = (float) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J13.toString());
        if (value == 0) return false;
        UnitNameEnum singleUnit = UnitNameEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J14.toString()));
        switch (singleUnit) {
            case TIME:  //秒
                if (((System.currentTimeMillis() - startTime) / 1000) > value) return true;
                break;
            case METER:  //米
                if (((carStateBean.getTotalMileage() - startMileage) / 1000) > value) return true;
                break;
        }
        return false;
    }

    @Override
    public boolean playFinishGearVoice(int operGear) {
        try {
            isPlay = true;
            String playContent = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J9.toString());
            playContent = playContent.replace("#", operGear + "");
            PlayerApi.playProjectAndJudgeVoice(context, new VoiceBean(playContent, ""), new IPlayerCallBack() {
                @Override
                public void playFinish() {
                    isPlay = false;
                }
            });
            //等待播报完成
            while (isPlay && autoJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
                Thread.sleep(100);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean jjdProjectIsOverTime(float startMileage, long startTime) {
        float value = (float) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J15.toString());
        if (value == 0) return false;
        UnitNameEnum totalUnit = UnitNameEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.J16.toString()));
        switch (totalUnit) {
            case TIME:  //秒
                if (((System.currentTimeMillis() - startTime) / 1000) > value) return true;
                break;
            case METER:  //米
                if (((carStateBean.getTotalMileage() - startMileage) / 1000) > value) return true;
                break;
        }
        return false;
    }
}
